﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormProject.Personel
{
    public partial class PersonelListesi : Form
    {
        int _SatirNo = 0;
        public PersonelListesi(int SatirNo)
        {
            InitializeComponent();
            _SatirNo = SatirNo;
        }
        
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        public BindingSource MySourceNew = null;//new BindingSource();
        public DataSet MyDataSet = new DataSet();

        public Form AcanForm = null;

        public DataTable PersonelGetir()
        {
            var sorgu = (from x in db.PersonelBilgileris select new { x.PersonelID,x.PersonelAdi,x.PersonelSoyadi,x.PersonelSubeID });
            SqlCommand cmd = db.GetCommand(sorgu) as SqlCommand;
            DataTable dt = new DataTable("PersonelBilgileri");
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            return dt;
        }

        private void PersonelBilgisiDoldur(int SubeID)
        {
            if (MyDataSet.Tables["PersonelBilgileri"] != null)
            {
                MyDataSet.Tables.Remove("PersonelBilgileri");
            }

            MyDataSet.Tables.Add(PersonelGetir());
            MySourceNew = new BindingSource();
            MySourceNew.DataSource = MyDataSet;
            MySourceNew.DataMember = MyDataSet.Tables["PersonelBilgileri"].TableName;
            string Filtre = "PersonelSubeID = " + SubeID + "";
            MySourceNew.Filter = Filtre;
            GrdPersonelListesi.DataSource = MySourceNew;
          
        
        }
        private void PersonelBilgisiDoldur()
        {
            if (MyDataSet.Tables["PersonelBilgileri"] != null)
            {
                MyDataSet.Tables.Remove("PersonelBilgileri");
            }

            MyDataSet.Tables.Add(PersonelGetir());
            MySourceNew = new BindingSource();
            MySourceNew.DataSource = MyDataSet;
            MySourceNew.DataMember = MyDataSet.Tables["PersonelBilgileri"].TableName;         
            GrdPersonelListesi.DataSource = MySourceNew;


        }
        
        private void PersonelListesi_Load(object sender, EventArgs e)
        {

           
            
            
            if (AcanForm.Name == "PersonelGirisCikisFrm")
            {   
              
                 PersonelGirisCikisFrm frm = Application.OpenForms["PersonelGirisCikisFrm"] as PersonelGirisCikisFrm;
                 PersonelBilgisiDoldur(Convert.ToInt32(frm.SubeID.SelectedValue));

                 //GrdPersonelListesi.DataSource = (from x in db.PersonelBilgileris where x.PersonelSubeID == Convert.ToInt32(frm.SubeID.SelectedValue) select new { x.PersonelID, x.PersonelAdi,x.PersonelSoyadi,x.Subeler.SubeAdi}).ToList();

            }
            else
            {
               PersonelBilgisiDoldur();
            }
           
        }

        private void GrdPersonelListesi_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (AcanForm.Name == "Personelfrm")
            {
                Personelfrm frm = Application.OpenForms["Personelfrm"] as Personelfrm;
                if (frm != null)
                {
                   
                    frm.PersonelID.Text = GrdPersonelListesi.Rows[e.RowIndex].Cells[0].Value.ToString();
                    frm.PersonelBilgisiGetir();
                    this.Close();
                }
            }
            else if (AcanForm.Name == "PersonelSafaatFrm")
            {
                PersonelSafaatFrm frm = Application.OpenForms["PersonelSafaatFrm"] as PersonelSafaatFrm;
                if (frm != null)
                {
                    frm.PersonelID.Text = GrdPersonelListesi.Rows[e.RowIndex].Cells[0].Value.ToString();
                    this.Close();
                }
 
            }
            else if (AcanForm.Name == "PersonelGirisCikisFrm")
            {
                PersonelGirisCikisFrm frm = Application.OpenForms["PersonelGirisCikisFrm"] as PersonelGirisCikisFrm;
                frm.GrdPersonelGirisCikis.Rows[_SatirNo].Cells[0].Value = GrdPersonelListesi.Rows[e.RowIndex].Cells[0].Value.ToString();
                this.Close();

            
            }
                
        }

        private void PersonelAdi_ara_TextChanged(object sender, EventArgs e)
        {
            if ( PersonelAdi_ara.Text.Trim().Length > 0)
            {
                string Filtre = "PersonelAdi Like '%" + PersonelAdi_ara.Text + "%' OR PersonelSoyadi Like '%" + PersonelAdi_ara.Text + "%'";
                MySourceNew.Filter = Filtre;
                MySourceNew.Position = MySourceNew.Count - 1;

            }
            else
            {
                MySourceNew.RemoveFilter();
                MySourceNew.Position = MySourceNew.Count - 1;
            }
        }
    }
}
